//
//  MaskTableViewController.swift
//  MyNotesDemo
//
//  Created by BZF on 2020/8/20.
//  Copyright © 2020 bzf. All rights reserved.
//

import UIKit

class MaskTableViewController: UITableViewController {
    
    var listData = NSMutableArray()

    override func viewDidLoad() {
        super.viewDidLoad()

        let dao = NoteDAO.sharedInstance
        self.listData = dao.findAll()
        
        //注册观察者 第二条备忘录
        NotificationCenter.default.addObserver(self, selector: #selector(reloadView(_:)), name: Notification.Name(rawValue: "reloadViewNotification"), object: nil)
    }
    
    @objc func reloadView(_ notification: Notification){
        let listData = notification.object as! NSMutableArray
        self.listData = listData
        self.tableView.reloadData()
    }

    // MARK: - Table view data source

    override func numberOfSections(in tableView: UITableView) -> Int {
        // #warning Incomplete implementation, return the number of sections
        return 1
    }

    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        // #warning Incomplete implementation, return the number of rows
        return self.listData.count
    }


    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        //返回指定重用标识符的单元格对象
        let cell = tableView.dequeueReusableCell(withIdentifier: "cellIdentifier", for: indexPath)
        
        let note = self.listData[indexPath.row] as! ENote
        cell.textLabel!.text = note.content
        cell.detailTextLabel!.text = note.date?.description

        return cell
    }
  

    // Override to support conditional editing of the table view.
    override func tableView(_ tableView: UITableView, canEditRowAt indexPath: IndexPath) -> Bool {
        // Return false if you do not want the specified item to be editable.
        return true
    }


    // MARK: - Navigation
    //页面将要跳转时调用
    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        if segue.identifier == "showDetail" {
            if let indexPath = self.tableView.indexPathForSelectedRow{ //indexPath不等于nil执行下面代码
                let note = self.listData[indexPath.row] as! ENote
                //获取要跳转的页面的视图控制器
                let controller = (segue.destination as! UINavigationController).topViewController as! DetailViewController
                
                controller.detailItem = note
                controller.navigationItem.leftBarButtonItem=self.splitViewController?.displayModeButtonItem
                controller.navigationItem.leftItemsSupplementBackButton = true
                    
            }
         
            
        }
    }


}
